package com.zsf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zsf.pojo.Product;
import com.zsf.pojo.Tag;
import com.zsf.pojo.User;
import com.zsf.service.ProductService;
import com.zsf.service.TagService;
import com.zsf.serviceImp.ProductServiceImp;
import com.zsf.serviceImp.TagServiceImp;

/**
 * Servlet implementation class ProductSeacherServlet
 */
@WebServlet("/seacher")
public class ProductSeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ProductService productService=new ProductServiceImp();
	TagService tagService=new TagServiceImp();
	Product product=new Product();
	Tag tag=new Tag();
	

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title=request.getParameter("title");
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int tag_id=Integer.valueOf(request.getParameter("tag"));
		User user=(User) request.getSession().getAttribute("current_user");
		PrintWriter pw=response.getWriter();
		product.setTitle(title);
		tag.setId(tag_id);
		product.setTag(tag);
		if(user==null){
			pw.print("<script>alert('请先登录！'); window.location.href='login.jsp';</script>");
		}else{
			List<Product> products=productService.getProductByTitle_Tag_Id(product);
			for(Product product1:products){
				System.out.println(product1);
			}
			request.setAttribute("products", products);
			request.getRequestDispatcher("productIndex.jsp").forward(request, response);
		}
	}


}
